import time
from selenium import webdriver
from selenium.webdriver.common.by import By


class Automation:
    '''
    自动化登录脚本
    '''
    # def login():
    #     # 创建数据库连接
    #     db = TryDButils.TryDButils()
    #     # 数据库查询账号密码
    #     date = db.select("select username,password from user")
    #
    #     # 循环输入账号密码登录
    #     for i in date:
    #         # 创建谷歌浏览器对象
    #         chromeDriver = webdriver.Chrome()
    #
    #         # 打开登录
    #         chromeDriver.get("http://localhost:90/")
    #
    #         # 窗口最大化
    #         chromeDriver.maximize_window()
    #
    #         # 通过前端id获取用户和密码框并填入数据
    #         chromeDriver.find_element(By.XPATH,"//*[@id='username']").send_keys(f"{i[0]}")
    #         # chromeDriver.find_element(By.ID, "username").send_keys(f"{i[0]}")
    #         chromeDriver.find_element(By.ID, "password").send_keys(f"{i[1]}")
    #
    #         # 点击登录按钮
    #         chromeDriver.find_element(By.ID, "login_btn").click()
    #
    #         time.sleep(3)
    #
    #         # 退出浏览器
    #         chromeDriver.quit()
    def login(self,username,password):

        # 创建谷歌浏览器对象
        chromeDriver = webdriver.Chrome()

        # 打开登录
        chromeDriver.get("http://localhost:90/")

        # 窗口最大化
        chromeDriver.maximize_window()

        # 通过前端id获取用户和密码框并填入数据
        chromeDriver.find_element(By.XPATH,"//*[@id='username']").send_keys(username)
        chromeDriver.find_element(By.ID, "password").send_keys(password)

        # 点击登录按钮
        chromeDriver.find_element(By.ID, "login_btn").click()

        # time.sleep(3)
        chromeDriver.set_page_load_timeout(2)
        # chromeDriver.implicitly_wait()
        # 截屏
        # chromeDriver.save_screenshot("demo/lisi---123456.jpg")
        result = chromeDriver.title
        # 退出浏览器
        chromeDriver.quit()
        return result


    '''
    自动化注册脚本
    '''
    def register(self,user,pwd):
            # 创建谷歌浏览器对象
            driver = webdriver.Chrome()

            # 打开注册页面
            driver.get("http://localhost:90/toregister.html")

            # 窗口最大化
            driver.maximize_window()

            # 填入数据
            driver.find_element(By.XPATH,"//*[@id='username']").send_keys(user)
            driver.find_element(By.XPATH,"//*[@id='password']").send_keys(pwd)
            driver.find_element(By.XPATH,"//*[@id='repassword']").send_keys(pwd)
            time.sleep(1)
            # 点击注册
            driver.find_element(By.XPATH,"//*[@id='login_btn']").click()

            time.sleep(1)
            result = driver.title
            # 退出浏览器
            driver.quit()
            return result
    # print(register('lisi0',123456))
    # def register():
    #     for i in range(10,25):
    #         # 创建谷歌浏览器对象
    #         driver = webdriver.Chrome()
    #
    #         # 打开注册页面
    #         driver.get("http://localhost:90/toregister.html")
    #
    #         # 窗口最大化
    #         driver.maximize_window()
    #
    #         # 填入数据
    #         driver.find_element(By.XPATH,"//*[@id='username']").send_keys(f"lisi{i}")
    #         driver.find_element(By.XPATH,"//*[@id='password']").send_keys(123456)
    #         driver.find_element(By.XPATH,"//*[@id='repassword']").send_keys(123456)
    #
    #         # 点击注册
    #         driver.find_element(By.XPATH,"//*[@id='login_btn']").click()
    #
    #         time.sleep(3)
    #
    #         # 退出浏览器
    #         driver.quit()

    # register()

    '''
    买入基金理财自动化脚本
    '''
    def pay(self,driver,path):
        # 创建谷歌浏览器对象
        # driver = webdriver.Chrome()
        #
        # # 打开登录页面登录
        # driver.get("http://localhost:90")
        # # 窗口最大化
        # driver.maximize_window()
        #
        # driver.find_element(By.XPATH, "//*[@id='username']").send_keys("lisi")
        # driver.find_element(By.ID, "password").send_keys(123456)
        # driver.find_element(By.ID, "login_btn").click()
        # time.sleep(1)

        #直接跳到零钱理财页面
        driver.get("http://localhost:90/user/finance/toChangeMoney.html")
        time.sleep(1)
        # 点击支付包零钱理财投资按钮 tr-[12345]
        driver.find_element(By.XPATH, f"{path}").click()
        time.sleep(1)
        # 输入支付密码

        driver.find_element(By.XPATH, "//*[@id='layui-layer1']/div[2]/input").send_keys(666666)
        time.sleep(1)
        # 点击确定
        driver.find_element(By.XPATH, "//*[@id='layui-layer1']/div[3]/a[1]").click()

        # el = driver.findelement(By.CSSSELECTOR, 'div.jconfirm-content div').text
        # assert  "买入失败!" == el
        # print(el)

        # 退出浏览器
        # driver.quit()

    '''
    添加银行卡信息
    '''
    # def Addcard():
    #     # 创建谷歌浏览器对象
    #     driver = webdriver.Chrome()
    #
    #     # 打开登录页面登录
    #     driver.get("http://localhost:90")
    #     # 窗口最大化
    #     driver.maximize_window()
    #     # 登录
    #     driver.find_element(By.XPATH, "//*[@id='username']").send_keys("lisi")
    #     driver.find_element(By.ID, "password").send_keys(123456)
    #     driver.find_element(By.ID, "login_btn").click()
    #     time.sleep(1)
    #     # 跳到银行卡管理页面
    #     driver.get("http://localhost:90/user/personal/toBankCard.html")
    #     # 点击添加按钮
    #     driver.find_element(By.XPATH,"//*[@id='bankCard_add_modal_btn']").click()
    #     time.sleep(1)
    #
    #     driver.find_element(By.XPATH,"//*[@id='cardBank_add_input']").send_keys('中国银行信用')
    #     time.sleep(1)
    #     # 选择信用卡
    #     driver.find_element(By.XPATH, "//*[@id='bankCardAddModal']/div/div/div[2]/form/div[2]/div[1]/div/label[2]").click()
    #     # ce = driver.find_element(By.XPATH,"//*[@id='bankCardAddModal']/div/div/div[2]/form/div[2]/div[1]/div/label[2]/input")
    #     # driver.execute_script("arguments[0].click()",ce)
    #     driver.find_element(By.XPATH,"//*[@id='cardNum_add_input']").send_keys('12345678901234567')
    #     time.sleep(1)
    #     # 点击保存
    #     driver.find_element(By.XPATH,"//*[@id='bankCard_save_btn']").click()
    #     time.sleep(2)
    #
    #     # 退出浏览器
    #     driver.quit()
    # # Addcard()

    # 登录方法
    def drivers(self):
        # 创建谷歌浏览器对象
        driver = webdriver.Chrome()

        # 打开登录页面登录
        driver.get("http://localhost:90")
        # 窗口最大化
        driver.maximize_window()
        # 登录
        driver.find_element(By.XPATH, "//*[@id='username']").send_keys("lisi")
        driver.find_element(By.ID, "password").send_keys(123456)
        driver.find_element(By.ID, "login_btn").click()
        time.sleep(1)
        return driver

    def Addcard(self,driver,cardBank,cardNum):
        # 创建谷歌浏览器对象
        # driver = webdriver.Chrome()

        # # 打开登录页面登录
        # driver.get("http://localhost:90")
        # # 窗口最大化
        # driver.maximize_window()
        # # 登录
        # driver.find_element(By.XPATH, "//*[@id='username']").send_keys("lisi")
        # driver.find_element(By.ID, "password").send_keys(123456)
        # driver.find_element(By.ID, "login_btn").click()
        # time.sleep(1)
        # 跳到银行卡管理页面
        driver.get("http://localhost:90/user/personal/toBankCard.html")
        # 点击添加按钮
        driver.find_element(By.XPATH,"//*[@id='bankCard_add_modal_btn']").click()
        time.sleep(1)

        driver.find_element(By.XPATH,"//*[@id='cardBank_add_input']").send_keys(cardBank)
        time.sleep(1)
        # 选择信用卡
        # ce = driver.find_element(By.XPATH,"//*[@id='bankCardAddModal']/div/div/div[2]/form/div[2]/div[1]/div/label[2]/input")
        # driver.execute_script("arguments[0].click()",ce)
        driver.find_element(By.XPATH, "//*[@id='bankCardAddModal']/div/div/div[2]/form/div[2]/div[1]/div/label[2]").click()
        driver.find_element(By.XPATH,"//*[@id='cardNum_add_input']").send_keys(cardNum)
        time.sleep(1)
        # 点击保存
        driver.find_element(By.XPATH,"//*[@id='bankCard_save_btn']").click()
        time.sleep(1)
        # 退出浏览器
        # driver.quit()

    '''
    银行卡删除
    '''
    def delCard(self):
        # 创建谷歌浏览器对象
        driver = webdriver.Chrome()

        # 打开登录页面登录
        driver.get("http://localhost:90")
        # 窗口最大化
        driver.maximize_window()
        # 登录
        driver.find_element(By.XPATH, "//*[@id='username']").send_keys("lisi")
        driver.find_element(By.ID, "password").send_keys(123456)
        driver.find_element(By.ID, "login_btn").click()
        time.sleep(1)
        # 跳到银行卡管理页面
        driver.get("http://localhost:90/user/personal/toBankCard.html")
        time.sleep(1)
        # 选择银行卡删除 默认第一张
        driver.find_element(By.XPATH,"/html/body/div/div/main/div/div/div[2]/div/div/div[1]/ul/li/button[2]").click()
        # / html / body / div / div / main / div / div / div[2] / div / div / div[1] / ul / li / button[2]
        # / html / body / div / div / main / div / div / div[3] / div / div / div[1] / ul / li / button[2]
        # / html / body / div / div / main / div / div / div[4] / div / div / div[1] / ul / li / button[2]
        time.sleep(1)
        # 点击确定
        driver.find_element(By.XPATH,"/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]").click()
        time.sleep(1)
        # 退出
        driver.quit()


    '''
    修改银行卡信息
    '''
    def update(self):
        # 创建谷歌浏览器对象
        driver = webdriver.Chrome()

        # 打开登录页面登录
        driver.get("http://localhost:90")
        # 窗口最大化
        driver.maximize_window()
        # 登录
        driver.find_element(By.XPATH, "//*[@id='username']").send_keys("lisi")
        driver.find_element(By.ID, "password").send_keys(123456)
        driver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        driver.find_element(By.PARTIAL_LINK_TEXT,"个人中心").click()
        time.sleep(1)
        driver.find_element(By.PARTIAL_LINK_TEXT,"银行卡管理").click()
        # 点击编辑  默认第一张
        driver.find_element(By.XPATH,"/html/body/div/div/main/div/div/div[2]/div/div/div[1]/ul/li/button[1]").click()
        time.sleep(2)
        # 修改信息
        name = driver.find_element(By.ID,"cardBank_update_input")
        name.clear()
        name.send_keys("中国建设银行")
        num = driver.find_element(By.ID,"cardNum_update_input")
        num.clear()
        num.send_keys(123456578987654321)
        # 点击确定
        driver.find_element(By.ID,"bankCard_update_btn").click()
        time.sleep(1)
        # 退出
        driver.quit()


    '''
    借网贷
    '''
    def daikuan(self):
        # 创建谷歌浏览器对象
        driver = webdriver.Chrome()

        # 打开登录页面登录
        driver.get("http://localhost:90")
        # 窗口最大化
        driver.maximize_window()
        # 登录
        driver.find_element(By.XPATH, "//*[@id='username']").send_keys("lisi")
        driver.find_element(By.ID, "password").send_keys(123456)
        driver.find_element(By.ID, "login_btn").click()
        time.sleep(1)
        # 点击金融工具，安全网贷
        driver.find_element(By.PARTIAL_LINK_TEXT, "金融工具").click()
        time.sleep(1)
        driver.find_element(By.PARTIAL_LINK_TEXT, "安全网贷").click()
        time.sleep(1)
        # 输入金额，期限
        driver.find_element(By.ID, "amount").send_keys(1000)
        driver.find_element(By.ID, "term").send_keys(20)
        # 点击申请
        driver.find_element(By.ID, "submit").click()
        time.sleep(1)
        # 点击确定
        driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]").click()
        time.sleep(1)
        # 退出
        driver.quit()


if __name__ == '__main__':
    a = Automation()

